package com.crm.system.service.impl.companyInvoiceManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.util.CommonUtils;
import com.crm.model.dto.CompanyInvoiceDTO;
import com.crm.model.entity.companyInvoiceManager.SysCompanyInvoice;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompany;
import com.crm.model.vo.companyInvoiceManager.CompanyInvoiceListPageVO;
import com.crm.model.vo.companyInvoiceManager.CompanyInvoiceVO;
import com.crm.model.vo.reminditem.RelatedRemindVO;
import com.crm.service.companyInvoiceManager.CompanyInvoiceDbService;
import com.crm.service.sys.permissionManagement.SysCompanyDbService;
import com.crm.system.service.companyInvoiceManager.CompanyInvoiceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author:chaifuyou
 * @Date: 2021/4/26 10:16
 */
@Service
public class CompanyInvoiceServiceImpl implements CompanyInvoiceService {

    @Autowired
    private CompanyInvoiceDbService companyInvoiceDbService;

    @Autowired
    private SysCompanyDbService sysCompanyDbService;

    /**
     * 根据当前登录人公司获取该公司可以选择开发票的公司列表
     * @return
     */
    @Override
    public List<CompanyInvoiceVO> getCompanyListByCompanyId() {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        //超级管理员
        if (CommonUtils.isAdmin(userAccount.getUserId())){
            List<CompanyInvoiceVO> resultList=new ArrayList<>();
            List<SysCompany> sysCompanyList = sysCompanyDbService.selectSysCompanyList();
            for (int i=0;i < sysCompanyList.size();i++){
                CompanyInvoiceVO vo =new CompanyInvoiceVO();
                vo.setUseCompanyId(sysCompanyList.get(i).getCompanyId());
                vo.setUseCompanyName(sysCompanyList.get(i).getCompanyName());
                resultList.add(vo);
            }
            return resultList;
        }
        List<CompanyInvoiceVO> resultList = companyInvoiceDbService.getCompanyListByCompanyId(userAccount.getCompanyId());
        //没有配置税盘的话就查询出当前用户的公司信息返回
       /* if (resultList.size() < 1 && Objects.nonNull(userAccount.getCompanyId())){
            SysCompany company = sysCompanyDbService.selectSysCompanyById(userAccount.getCompanyId());
            CompanyInvoiceVO vo = new CompanyInvoiceVO();
            vo.setUseCompanyName(company.getCompanyName());
            vo.setUseCompanyId(company.getCompanyId());
            resultList.add(vo);
        }*/
        return resultList;
    }

    /**
     * 税盘管理列表
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param companyName
     * @return
     */
    @Override
    public DhPageInfo<CompanyInvoiceListPageVO> getCompanyList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String companyName) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<CompanyInvoiceListPageVO> resultList = companyInvoiceDbService.getCompanyList(companyName);
        PageInfo<CompanyInvoiceListPageVO> pageInfo = new PageInfo<CompanyInvoiceListPageVO>(resultList);
        return new DhPageInfo<CompanyInvoiceListPageVO>(pageInfo, resultList);
    }

    /**
     * 根据公司id 删除税盘设置
     * @param companyId
     */
    @Override
    public void delTaxDisc(String companyId) {
        companyInvoiceDbService.delTaxDisc(companyId);
    }

    /**
     * 添加税盘
     * @param dto
     * @Author chaifuyou
     * @return
     */
    @Override
    public boolean saveTaxDisc(CompanyInvoiceDTO dto) {
        //查询是否有税盘
        Integer taxCount = companyInvoiceDbService.selectTaxCountByCompanyId(dto.getCompanyId());
        if (taxCount > 0){
            return false;
        }
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        SysCompanyInvoice sysCompanyInvoice = new SysCompanyInvoice();
        sysCompanyInvoice.setCompanyId(dto.getCompanyId());
        sysCompanyInvoice.setCreatePeopleId(userAccount.getAccountId());
        sysCompanyInvoice.setCreateTm(new Date());
        for (int i=0;i<dto.getUseCompanyIds().size();i++){
            sysCompanyInvoice.setCompanyInvoiceId(CommonUtils.getGUID());
            sysCompanyInvoice.setUseCompanyId(dto.getUseCompanyIds().get(i));
            companyInvoiceDbService.saveTaxDisc(sysCompanyInvoice);
        }
        SysCompany sysCompany =sysCompanyDbService.selectSysCompanyById(dto.getCompanyId());
        //修改公司税盘编号
        if (!dto.getTaxNumber().equals(sysCompany.getTaxNumber())){
            sysCompany.setTaxNumber(dto.getTaxNumber());
            sysCompany.setUpdatePeopleId(userAccount.getAccountId());
            sysCompany.setUpdateTm(new Date());
            sysCompanyDbService.updateCompanyTaxNumber(sysCompany);
        }
        return true;
    }

    /**
     * 修改税盘
     * @param dto
     */
    @Override
    public void updateTaxDisc(CompanyInvoiceDTO dto) {
        companyInvoiceDbService.delTaxDisc(dto.getCompanyId());
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        SysCompanyInvoice sysCompanyInvoice = new SysCompanyInvoice();
        sysCompanyInvoice.setCompanyId(dto.getCompanyId());
        sysCompanyInvoice.setCreatePeopleId(userAccount.getAccountId());
        sysCompanyInvoice.setCreateTm(new Date());
        for (int i=0;i<dto.getUseCompanyIds().size();i++){
            sysCompanyInvoice.setCompanyInvoiceId(CommonUtils.getGUID());
            sysCompanyInvoice.setUseCompanyId(dto.getUseCompanyIds().get(i));
            companyInvoiceDbService.saveTaxDisc(sysCompanyInvoice);
        }
        SysCompany sysCompany =sysCompanyDbService.selectSysCompanyById(dto.getCompanyId());
        //修改公司税盘编号
        if (!dto.getTaxNumber().equals(sysCompany.getTaxNumber())){
            sysCompany.setTaxNumber(dto.getTaxNumber());
            sysCompany.setUpdatePeopleId(userAccount.getAccountId());
            sysCompany.setUpdateTm(new Date());
            sysCompanyDbService.updateCompanyTaxNumber(sysCompany);
        }
    }
}
